package com.song.common.config.web;

import com.song.common.constant.DatePatternConstant;
import com.song.common.constant.DateRegex;
import com.song.common.util.LocalDateTimeUtil;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.time.LocalTime;

@Component
public class LocalTimeConverter implements Converter<String, LocalTime> {
    @Override
    public LocalTime convert(String source) {
        String value = source.trim();
        if ("".equals(value)) {
            return null;
        }
        if (source.matches(DateRegex.HOUR + ":" + DateRegex.MINUTE + ":" + DateRegex.SECOND)){
            return LocalDateTimeUtil.stringToLocalTime(source, DatePatternConstant.DEFAULT_TIME_PATTERN);
        }else {
            throw new IllegalArgumentException("Invalid date value '" + source + "'");
        }
    }
}
